package com.atguigu.gmall.product.controller.admin;

import com.atguigu.gmall.common.constant.ProductConstants;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product")
public class SkuInfoController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 商品下架
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        //参数校验
        if(null == skuId){
            throw new GmallException("参数错误！",null);
        }

        //2 调用业务层方法
        skuInfoService.upOrDown(skuId,ProductConstants.SKU_DOWN_SALE);

        //3 响应数据
        return Result.ok();
    }

    /**
     * 商品上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        //1 校验参数
        if(null == skuId){
            throw new GmallException("参数错误！",null);
        }

        //2 调用业务层方法
        skuInfoService.upOrDown(skuId, ProductConstants.SKU_UP_SALE);

        //3 响应数据
        return Result.ok();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param size
     * @return
     */
    @GetMapping("/list/{pageNum}/{size}")
    public Result list(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("size") Integer size){
        //1 校验参数
        //2 调用业务层方法
        IPage<SkuInfo> skuInfoIPage = skuInfoService.listPage(pageNum,size);

        //3 响应数据
        return Result.ok(skuInfoIPage);
    }

    /**
     * 保存数据
     *
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        //1 校验参数
        if (null == skuInfo) {
            throw new GmallException("参数错误!", null);
        }

        //1 调用业务层方法
        skuInfoService.save(skuInfo);

        //2 响应数据
        return Result.ok();
    }
}
